<?php

/**
 * This is the model class for table "post".
 *
 * The followings are the available columns in table 'post':
 * @property integer $post_id
 * @property string $post_title
 * @property string $post_content
 * @property integer $post_prv_id
 * @property integer $post_dis_id
 * @property integer $post_prj_id
 * @property integer $post_price
 * @property integer $post_vip
 * @property integer $post_area
 * @property string $post_address
 * @property integer $post_date_create
 * @property integer $post_date_update
 * @property string $post_phone_contact
 * @property integer $post_active
 */
class Post extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'post';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('post_title, post_content, post_prv_id, post_dis_id, post_cat_id, post_use_id, post_active', 'required',
               'message' => "{attribute} không được bỏ trống",'on' => 'insert,update'),
			array('post_prv_id, post_dis_id, post_prj_id, post_cat_id, post_use_id, post_price, post_vip, post_area, post_date_create, post_date_update, post_active', 'numerical', 'integerOnly'=>true),
			array('post_title, post_address, post_avatar, post_phone_contact', 'length', 'max'=>255),
         
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('post_id, post_title, post_content, post_prv_id, post_dis_id, post_prj_id, post_price, post_vip, post_area, post_address, post_date_create, post_date_update, post_phone_contact, post_active', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
         'district' => array(self::BELONGS_TO,'District','post_dis_id'),
         'province' => array(self::BELONGS_TO,'Province','post_prv_id'),
         'project' => array(self::BELONGS_TO,'Project','post_prj_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'post_id' => 'ID',
			'post_title' => 'Tiêu đề',
			'post_content' => 'Nội dung',
			'post_prv_id' => 'Tỉnh/TP',
			'post_dis_id' => 'Quận/Huyện',
         'post_prj_id' => 'Dự án',
         'post_cat_id' => 'Chuyên mục',
         'post_use_id' => 'Người đăng',
			'post_price' => 'Giá',
			'post_vip' => 'Tin VIP',
			'post_area' => 'Diện tích',
         'post_avatar' => 'Ảnh đại diện',
			'post_address' => 'Địa chỉ',
			'post_date_create' => 'Ngày tạo',
			'post_date_update' => 'Ngày làm mới',
			'post_phone_contact' => 'SĐT liên lạc',
			'post_active' => 'Hiển thị',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('post_id',$this->post_id);
		$criteria->compare('post_title',$this->post_title,true);
		$criteria->compare('post_content',$this->post_content,true);
		$criteria->compare('post_prv_id',$this->post_prv_id);
		$criteria->compare('post_dis_id',$this->post_dis_id);
      $criteria->compare('post_prj_id',$this->post_prj_id);
      $criteria->compare('post_cat_id',$this->post_cat_id);
      $criteria->compare('post_use_id',$this->post_use_id);
		$criteria->compare('post_price',$this->post_price);
		$criteria->compare('post_vip',$this->post_vip);
		$criteria->compare('post_area',$this->post_area);
		$criteria->compare('post_address',$this->post_address,true);
		$criteria->compare('post_date_create',$this->post_date_create);
		$criteria->compare('post_date_update',$this->post_date_update);
		$criteria->compare('post_phone_contact',$this->post_phone_contact,true);
		$criteria->compare('post_active',$this->post_active);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Post the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
